package com.shopping.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shopping.po.Address;
import com.shopping.po.User;
import com.shopping.po.UserInfor;
import com.shopping.service.AddressService;

@Controller
public class AddressController {
	@Autowired
	private AddressService addressService;
	
	@RequestMapping("/lookAddress")
	public String lookAddress(HttpSession session,Model model) {
		User user = (User) session.getAttribute("user");
		Integer user_id = user.getUser_id();
		List<Address> address = addressService.selectAddressById(user_id);
		model.addAttribute("address",address);
		return "address";
	}
	@RequestMapping("/insertAddress")
	public String insertAddress(Address address,HttpSession session,Model model) {
		User user = (User) session.getAttribute("user");
		String username = user.getUsername();
		Integer user_id = user.getUser_id();
		String adr = address.getAdr();
		String detail = address.getDetail();
		String name = address.getName();
		String phone = address.getPhone();
		String mail = address.getMail();
		addressService.insertAddress(adr,detail,name,phone,mail,user_id);
		return "redirect:/lookAddress";
	}
	
	@RequestMapping("/deleteAddress")
	public String deleteAddress(Integer address_id) {
		addressService.deleteAddress(address_id);
		return "redirect:/lookAddress";
	}
	
	@RequestMapping("/updateaddress")
	public String updateaddress(Integer address_id,Model model) {
		System.out.println(address_id+"*************");
		Address address = addressService.selectAddressByAId(address_id);
		System.out.println(address);
		model.addAttribute("address1",address);
		return "updateAddress";
	}
	
	@RequestMapping("/updateAddress")
	public String updateAddress(Integer address_id,Model model,Address address) {
		String adr = address.getAdr();
		String detail = address.getDetail();
		String name = address.getName();
		String phone = address.getPhone();
		String mail = address.getMail();
		addressService.updateAddressByAId(adr,detail,name,phone,mail,address_id);
		return "redirect:/lookAddress";
	}

}
